package entreprise;

import java.util.ArrayList;

import database.Database;
import mapping.DBReservation;

public class Reservation extends DBReservation {
	private Database database;

	public Reservation() {
		super();
		database = new Database("restoreservation");
	}

	public Reservation(Database database) {
		super();
		database = new Database("restoreservation");
		this.database = database;
	}

	public Reservation(String id_reservation) {
		super(id_reservation);
	}

	public Reservation(String id_food, String id_client, String amount,
			String time_reservation, String time_eat) {
		super(id_food, id_client, amount, time_reservation, time_eat);
	}

	public Reservation(String id_reservation, String id_food, String id_client,
			String amount, String time_reservation, String time_eat) {
		super(id_reservation, id_food, id_client, amount, time_reservation,
				time_eat);
	}

	public Database getDatabase() {
		return database;
	}

	public void setDatabase(Database database) {
		this.database = database;
	}

	public boolean addReservation() {
		String sql_str = "";
		try {
			sql_str = "INSERT INTO reservation(id_client, id_food, amount, date_eat, time_eat) VALUES("
					+ Integer.parseInt(this.getId_client())
					+ ", "
					+ Integer.parseInt(this.getId_food())
					+ ", "
					+ Double.parseDouble(this.getAmount())
					+ ", '"
					+ this.getDate_eat() + "', '" + this.getTime_eat() + "')";
			database.set(sql_str);

		} catch (Exception ex) {
			ex.printStackTrace();
			return false;
		}
		return true;
	}

	public boolean updateReservation() {
		String sql_str = "";
		try {
			sql_str = "UPDATE reservation set id_client = "
					+ Integer.parseInt(this.getId_client()) + ", id_food = "
					+ Integer.parseInt(this.getId_food()) + ", amount = "
					+ Double.parseDouble(this.getAmount()) + ", date_eat = '"
					+ this.getDate_eat() + "', time_eat = '"
					+ this.getTime_eat() + "' WHERE id_reservation = "
					+ Integer.parseInt(this.getId_reservation());
			database.set(sql_str);
		} catch (Exception ex) {
			ex.printStackTrace();
			return false;
		}
		return true;
	}

	public boolean deleteReservation() {
		String sql_str = "";
		try {
			sql_str = "DELETE FROM reservation WHERE id_reservation = "
					+ Integer.parseInt(this.getId_reservation());
			database.set(sql_str);
		} catch (Exception ex) {
			ex.printStackTrace();
			return false;
		}

		return true;
	}

	public ArrayList<ArrayList<String>> listAllReservation() {
		String str_sql = "select * from reservation";
		ArrayList<ArrayList<String>> datas = new ArrayList<ArrayList<String>>();
		datas = database.get(str_sql);
		return datas;
	}

	public ArrayList<String> listReservationWithID(String id_reservation) {
		String str_sql = "select * from food where id_reservation = "
				+ Integer.parseInt(id_reservation);
		ArrayList<ArrayList<String>> datas = new ArrayList<ArrayList<String>>();
		ArrayList<String> data = new ArrayList<String>();
		datas = database.get(str_sql);
		if (datas.size() > 0)
			data = datas.get(0);
		return data;
	}

	public ArrayList<ArrayList<String>> listReservationWithIdClient(
			String id_client) {
		String str_sql = "select * from reservation where id_client = '"
				+ id_client + "'";
		ArrayList<ArrayList<String>> datas = new ArrayList<ArrayList<String>>();
		datas = database.get(str_sql);
		return datas;
	}

	public ArrayList<ArrayList<String>> listReservation(String id_client,
			String date_eat, String time_eat) {
		String str_sql = "select * from reservation where id_client = '"
				+ id_client + "' and date_eat = '" + date_eat
				+ "' and time_eat = '" + time_eat + "'";
		ArrayList<ArrayList<String>> datas = new ArrayList<ArrayList<String>>();
		datas = database.get(str_sql);
		return datas;
	}

	public ArrayList<ArrayList<String>> listClientReserve() {
		String str_sql = "select a.id_client, a.name_client, a.numberphone, a.email, date_eat, time_eat, sum(b.amount*c.price) as price_total "
				+ "from client a, reservation b, food c "
				+ "where a.id_client = b.id_client "
				+ "and b.id_food = c.id_food "
				+ "group by a.id_client, a.name_client, a.numberphone, a.email, date_eat, time_eat";
		ArrayList<ArrayList<String>> datas = new ArrayList<ArrayList<String>>();
		datas = database.get(str_sql);
		return datas;
	}

}
